﻿using System;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI;

namespace VPLink.WebParts
{
    public class ListViewToolPart : ToolPart
    {
        private DropDownList dpList;

        /// <summary>
        /// Constructor for the class.
        /// </summary>        
        public ListViewToolPart()
        {
            this.Title = "Liste affichée :";
            this.PreRender += new EventHandler(ListViewToolPart_PreRender);
        }

        /// <summary>
        /// Create dropdownlist to display lists
        /// </summary>
        override protected void CreateChildControls()
        {
            try
            {
                ListViewWebPart currentWebPart = (ListViewWebPart)ParentToolPane.SelectedWebPart;

                #region List

                Label lblTitre = new Label();
                lblTitre.Text = "Choisissez la liste qui sera affichée";
                Controls.Add(lblTitre);

                LiteralControl lit2 = new LiteralControl("<BR>&nbsp;&nbsp;&nbsp;");
                Controls.Add(lit2);

                dpList = new DropDownList();
                Guid siteID = SPContext.Current.Site.ID;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(siteID))
                    {
                        foreach (SPWeb web in site.AllWebs)
                        {
                            foreach (SPList content in web.Lists)
                            {
                                //if (((int)content.BaseTemplate) == 104 || ((int)content.BaseTemplate) == 103 || ((int)content.BaseTemplate) > 10000)
                                if (((int)content.BaseTemplate) == currentWebPart.ListTemplate || ((int)content.BaseTemplate) > 10000 || (int)content.BaseTemplate == 100 || (int)content.BaseTemplate == 104)
                                {
                                    if (web != SPContext.Current.Web)
                                    {
                                        dpList.Items.Add(new ListItem(content.Title + " (" + web.Title + ") ", content.ID.ToString() + ";" + web.ID.ToString()));
                                    }
                                    else
                                        dpList.Items.Add(new ListItem(content.Title + " (" + web.Title + ") ", content.ID.ToString()));
                                }
                            }

                            web.Dispose();
                        }
                    }
                });
                if (dpList.Items.Count > 0)
                {
                    dpList.Enabled = true;
                    dpList.Visible = true;
                }
                Controls.Add(dpList);

                #endregion
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        ///	<summary>
        ///	This method is called by the ToolPane object to apply property changes to the selected Web Part. 
        ///	</summary>
        override public void ApplyChanges()
        {
            // Apply property values here.
            ListViewWebPart currentWebPart = (ListViewWebPart)ParentToolPane.SelectedWebPart;
            if (currentWebPart != null)
            {
                string[] ids = this.dpList.SelectedItem.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                currentWebPart.ListID = new Guid(ids[0]);
                currentWebPart.WebID = (ids.Length == 2) ? new Guid(ids[1]) : Guid.Empty;
            }
        }

        ///	<summary>
        ///	Initializes ToolPart values. 
        ///	</summary>
        private void ListViewToolPart_PreRender(object sender, EventArgs ea)
        {
            try
            {
                ListViewWebPart currentWebPart = (ListViewWebPart)ParentToolPane.SelectedWebPart;
                if (currentWebPart != null)
                {
                    Guid activeWeb = currentWebPart.WebID;
                    Guid activeList = currentWebPart.ListID;
                    if (activeList != Guid.Empty)
                    {
                        if (activeWeb != Guid.Empty)
                            this.dpList.SelectedValue = activeList.ToString() + ";" + activeWeb.ToString();
                        else
                            this.dpList.SelectedValue = activeList.ToString();
                    }
                }
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }
    }
}
